package com.recsmile.sgs.client.operations;

import java.util.ArrayList;
import java.util.List;

import com.kael.sgs.core.Command;
import com.recsmile.sgs.client.card.Card;
import com.recsmile.sgs.client.commonds.DiscardInGameServerCommand;
import com.recsmile.sgs.client.entity.Activatable;
import com.recsmile.sgs.client.entity.Hero;
import com.recsmile.sgs.client.ui.components.CardUI;
import com.recsmile.sgs.client.ui.components.ClientGameUI;
import com.recsmile.sgs.client.ui.components.EquipmentUI;
import com.recsmile.sgs.client.ui.components.GamePanel;
import com.recsmile.sgs.client.ui.components.HeroUI;
import com.recsmile.sgs.client.ui.components.PlayerUI;

public class DiscardOperation implements Operation {
	
	private GamePanel<? extends Hero> panel;
	private List<CardUI> cards = new ArrayList<CardUI>();
	private final int amount;
	
	public DiscardOperation(int amount) {
		this.amount = amount;
	}
	
	@Override
	public void onConfirmed() {
		for(CardUI cardUI : panel.getContent().getCardRackUI().getCardUIs()) {
			cardUI.setActivatable(false);
		}
		panel.getContent().setConfirmEnabled(false);
		List<Card> cs = new ArrayList<Card>();
		for(CardUI ui : cards){
			cs.add(ui.getCard());
		}
		
		panel.getChannel().send(new DiscardInGameServerCommand(cs));
	}

	@Override
	public void onCardClicked(CardUI card) {
		if (cards.remove(card)) {
			card.setActivated(false);
			if (cards.size() == 0) {
				panel.getContent().setConfirmEnabled(false);
			}
		} else {
			card.setActivated(true);
			if (cards.size() == 0) {
				panel.getContent().setConfirmEnabled(true);
			}
			cards.add(card);
			if (cards.size() > amount) {
				cards.get(0).setActivated(false);
				cards.remove(0);
			}
		}
	}

	@Override
	public void onActivated(GamePanel<? extends Hero> panel, Activatable source) {
		this.panel = panel;
		ClientGameUI<? extends Hero> panelUI = panel.getContent();
		panelUI.showCountdownBar();
		for(CardUI cardUI : panelUI.getCardRackUI().getCardUIs()) {
			cardUI.setActivatable(true);
		}
	}

	@Override
	public void onPlayerClicked(PlayerUI player) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSelfClicked(HeroUI<? extends Hero> self) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onEquipmentClicked(EquipmentUI equipment) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCanceled() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onEnded() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Command getNext() {
		// TODO Auto-generated method stub
		return null;
	}

}
